package com.cn.kehong.service;


import com.cn.kehong.api.IDataSubmissionKbService;
import com.cn.kehong.api.IDataSubmissionYyService;
import com.cn.kehong.dao.DataSubmissionKbMapper;
import com.cn.kehong.dao.DataSubmissionMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataSubmissionKbServiceImpl implements IDataSubmissionKbService {

    @Autowired
    private DataSubmissionKbMapper dataSubmissionKbMapper;
    @Autowired
    private IDataSubmissionYyService dataSubmissionYyService;
    @Autowired
    private DataSubmissionMapper dataSubmissionMapper;

    @Override
    public List<DataSubmissionVo> getDataSubmissionVoListByKb(QueryForm queryForm) throws Exception {
        int count = dataSubmissionKbMapper.getDataSubmissionVoListByKbCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<DataSubmissionVo> dataSubmissions = dataSubmissionKbMapper.getDataSubmissionVoListByKbList(queryForm);
        dataSubmissions.get(0).setCount(count);
        return dataSubmissions;
    }

    @Override
    public String addDataSubmissionKb(List<DataSubmissionKb> dataSubmissionKbs) throws Exception {
        if(CollectionUtils.isEmpty(dataSubmissionKbs)){
            return "数组为空！";
        }
        int count = dataSubmissionKbMapper.batchInsertDataSubmissionKb(dataSubmissionKbs);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDataSubmissionKb(List<DataSubmissionKb> dataSubmissionKbs) throws Exception {
        if(CollectionUtils.isEmpty(dataSubmissionKbs)){
            return "数组为空！";
        }
        String submissionId = dataSubmissionKbs.get(0).getSubmissionId();
        if(StringUtils.isEmpty(submissionId)){
            return "id 为空！";
        }
        DataSubmissionKbExample example = new DataSubmissionKbExample();
        example.createCriteria().andSubmissionIdEqualTo(submissionId);
        int count = dataSubmissionKbMapper.deleteByExample(example);
        if(count < 0){
            return "清理数据异常！";
        }
        for(DataSubmissionKb data:dataSubmissionKbs){
            data.setId(CommonUtil.generateRandomNum("submissionKb-"));
            data.setSubmissionId(submissionId);
            data.setStatus("0");
            data.setStoreType(dataSubmissionKbs.get(0).getStoreType());
        }
        count = dataSubmissionKbMapper.batchInsertDataSubmissionKb(dataSubmissionKbs);
        if(count < 1){
            throw new Exception("插入数据异常！");
        }
        return "";
    }

    @Override
    public List<DataSubmissionKb> getDataSubmissionKbListById(String id, String dataType) throws Exception {
        if(StringUtils.isEmpty(id)){
            return Lists.newArrayList();
        }
        DataSubmissionKbExample kbExample = new DataSubmissionKbExample();
        kbExample.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andSubmissionIdEqualTo(id).andStoreTypeEqualTo(dataType);
        kbExample.setOrderByClause("create_time desc");
        List<DataSubmissionKb> list = dataSubmissionKbMapper.selectByExample(kbExample);
        return list;
    }

    @Override
    public List<DataSubmissionVo> getAnalysisListByKb(QueryForm queryForm) throws Exception {
        List<DataSubmissionVo> dataSubmissions = dataSubmissionKbMapper.getAnalysisListByKb(queryForm);
        return dataSubmissions;
    }

    @Override
    public List<DataSubmissionVo> getAnalysisListByYy(QueryForm queryForm) throws Exception {
        List<DataSubmissionVo> dataSubmissions = dataSubmissionMapper.getAnalysisListByYy(queryForm);
        return dataSubmissions;
    }


}
